package Backtracking;

import java.util.*;

public class 非递减子序列 {

    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] nums;
    int len;

    public List<List<Integer>> findSubsequences(int[] nums) {
        this.nums = nums;
        len = nums.length;
        backTracking(0);
        return res;
    }

    private void backTracking(int index) {
        Set<Integer> set = new HashSet<>();
        for (int i = index; i < len; i++) {
            if (!path.isEmpty() && nums[i] < path.getLast() || set.contains(nums[i])) continue;
            set.add(nums[i]);
            path.add(nums[i]);
            if (path.size() >= 2) res.add(new ArrayList<>(path));
            backTracking(i + 1);
            path.removeLast();
        }
    }
}
